package com.hongtech.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hongtech.common.service.impl.CrudServiceImpl;
import com.hongtech.common.util.CommonUtil;
import com.hongtech.modules.system.dto.SysUserDTO;
import com.hongtech.modules.system.entity.SysUserEntity;
import com.hongtech.modules.system.entity.SysUserRoleEntity;
import com.hongtech.modules.system.mapper.SysUserMapper;
import com.hongtech.modules.system.mapper.SysUserRoleMapper;
import com.hongtech.modules.system.service.SysUserService;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * SysUser 表数据服务层接口实现类
 */
@Service
public class SysUserServiceImpl extends CrudServiceImpl<SysUserMapper, SysUserEntity, SysUserDTO>
        implements SysUserService {

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Override
    public void insertUser(SysUserEntity user, Long[] roleIds) {
        user.setPassword(CommonUtil.MD5(user.getPassword()));
        userMapper.insert(user);
        // 绑定角色
        if (ArrayUtils.isNotEmpty(roleIds)) {
            for (Long rid : roleIds) {
                SysUserRoleEntity sysUserRoleEntity = new SysUserRoleEntity();
                sysUserRoleEntity.setUserId(user.getId());
                sysUserRoleEntity.setRoleId(rid);
                userRoleMapper.insert(sysUserRoleEntity);
            }
        }

    }

    @Override
    public void updateUser(SysUserEntity sysUserEntity, Long[] roleIds) {
        sysUserEntity.setPassword(null);
        // 更新用户
        userMapper.updateById(sysUserEntity);
        // 删除已有权限
        LambdaQueryWrapper<SysUserRoleEntity> deleteWrapper = new LambdaQueryWrapper<>();
        deleteWrapper.eq(SysUserRoleEntity::getUserId, sysUserEntity.getId());
        userRoleMapper.delete(deleteWrapper);
        // 重新绑定角色
        if (ArrayUtils.isNotEmpty(roleIds)) {
            for (Long rid : roleIds) {
                SysUserRoleEntity sysUserRoleEntity = new SysUserRoleEntity();
                sysUserRoleEntity.setUserId(sysUserEntity.getId());
                sysUserRoleEntity.setRoleId(rid);
                userRoleMapper.insert(sysUserRoleEntity);
            }
        }
    }

    @Override
    public SysUserEntity login(String userName, String password) {
        LambdaQueryWrapper<SysUserEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserEntity::getUserName, userName);
        queryWrapper.eq(SysUserEntity::getPassword, CommonUtil.MD5(password));
        queryWrapper.last("LIMIT 1");
        return this.getOne(queryWrapper);
    }

    @Override
    public List<Map<Object, Object>> listUserPage(String search) {
        return baseMapper.listUser(search);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        // 删除用户与角色关联
        LambdaQueryWrapper<SysUserRoleEntity> deleteWrapper = new LambdaQueryWrapper<>();
        deleteWrapper.eq(SysUserRoleEntity::getUserId, id);
        userRoleMapper.delete(deleteWrapper);
        this.removeById(id);
    }

}
